﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Text.RegularExpressions;

namespace BTS.COMMON
{
    /// <summary>
    /// 邮件发送类 
    /// </summary>
    public class EmailHelper
    {
        /// <summary>
        /// 单封邮件发送
        /// </summary>
        /// <param name="strTo">收件人地址(可以是多个地址用"|"/","/";"隔开)</param>
        /// <param name="strTitle">邮件标题</param>
        /// <param name="strBody">邮件内容</param>
        /// <param name="strIsHTML">是否是HTML邮件</param>
        /// <param name="strFrom">发件人地址</param>
        /// <param name="strLoginName">发件人名称</param>
        /// <param name="strPassword">发件人邮箱密码</param>
        /// <param name="strSMTP">发件人SMTP</param>
        /// <returns></returns>
        public static string SendMail(string strTo, string strTitle, string strBody, bool strIsHTML, string strFrom, string strLoginName, string strPassword, string strSMTP)
        {
            System.Threading.Thread.Sleep(500);

            string strCount = "";
            if (!IsEmail(strFrom))
            {
                strCount = "发件人邮箱格式不正确";
                return strCount;
            }

            string[] str = strTo.Split(new char[] { ',', ';', '|' });
            for (int i = 0; i < str.Length; i++)
            {
                if (IsEmail(str[i]))
                {
                    MailMessage msg = new MailMessage();
                    msg.From = new MailAddress(strFrom, strLoginName, System.Text.Encoding.UTF8);
                    /* 上面3个参数分别是发件人地址，发件人姓名，编码*/
                    msg.Subject = strTitle;                             //邮件标题 
                    msg.SubjectEncoding = System.Text.Encoding.UTF8;    //邮件标题编码 
                    msg.Body = strBody;                                 //邮件内容 
                    msg.BodyEncoding = System.Text.Encoding.UTF8;       //邮件内容编码 
                    msg.IsBodyHtml = strIsHTML;                         //是否是HTML邮件 
                    msg.Priority = MailPriority.High;                   //邮件优先级 Normal
                    SmtpClient client = new SmtpClient();
                    client.Credentials = new System.Net.NetworkCredential(strFrom, strPassword);
                    client.DeliveryMethod = SmtpDeliveryMethod.Network;
                    client.Host = strSMTP;
                    object userState = msg;
                    msg.To.Add(str[i].ToString());

                    try
                    {
                        System.Threading.Thread.Sleep(100);
                        client.Send(msg);
                        strCount = "发送成功";
                    }
                    catch (SmtpException ex)
                    {
                        strCount = "发送失败。错误信息：" + ex.Message;
                    }
                }
            }
            return strCount;
        }

        /// <summary>
        /// 判断输入的字符串是否是一个合法的Email地址
        /// </summary>
        public static bool IsEmail(string input)
        {
            string pattern = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
            Regex regex = new Regex(pattern);
            return regex.IsMatch(input);
        }

    }
}
